﻿$(function ()
{
    var notRated = false;

    $.ajax({
        async: false,
        type: "POST",
        url: '/tools/israted',
        data:
        {
            name: $('input[name=User]').attr('value'),
            type: $('input[name=Type]').attr('value'),
            id: $('input[name=EntryId]').attr('value')
        },
        success: function (message)
        {
            if (message.success == true)
                notRated = !message.rated;
        }
    });

    if (notRated == true)
    {
        $('.star').rating('readOnly', false);
        $('#rater').hide();
        $('.star').on('click', function () {
            var value = $(this).find('a').text();
            var objId = $('input[name=EntryId]').attr('value');
            var objType = $('input[name=Type]').attr('value');

            $.ajax({
                type: "POST",
                url: '/tools/setratingfixed',
                data: { number: value, id: objId, type: objType },
                success: function (response) {
                    if (response.success != false) {
                        $('#currentlyrated').html('Currently rated ' + response.totalRating + ' by ' + response.totalPeople + ' people');
                        var res = (response.totalRating / 5) * 20;
                        $('.star').rating('select', res - 1);
                        $('.star').rating('readOnly', true);
                    }
                },
                error: function () {
                    alert('There was an error.');
                }
            });
        });
    }
    else
    {
        $('.star').rating('readOnly', true);
        $('#currentlyrated').remove();
        $('#rater').hide();
    }

    $('.rating-cancel').each(function () {$(this).remove();});

});